﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tour.BLL;
using Tour.DAL;

public partial class Admin_Modules_Room_RoomTypeList : VST.Controls.BaseLCGPage
{
    public int? RoomTypeId { get; set; }
    public bool? IsVisible { get; set; }
    public bool? IsHome { get; set; }
    public string Expression { get; set; }
    public string Collate { get; set; }
    public static int PageSize = 10;
    public int PageIndex { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetPropertyFromQueryString();
            LoadData();            
        }
    }
    void GetPropertyFromQueryString()
    {
        IsVisible = CommonHelper.QueryStringBool("IsVisible");
        IsHome = CommonHelper.QueryStringBool("IsHome");
    }
    void LoadData()
    {
        int TotalRow = 0;
        //grv.DataSource = TblRoomTypeManager.CreateInstant().SelectTblRoomTypeBy(null, RoomTypeId, "", IsHome, IsVisible, PageSize, PageIndex, out TotalRow, "CreatedDate", "DESC");
        grv.DataSource = TblRoomTypeManager.CreateInstant().SelectTblRoomTypeBy(null,null, null,"", true,true,"CreatedDate", "DESC");
        grv.DataBind();
        smartPager.PageSize = PageSize;
        smartPager.QueryStringKey = "PageIndex";
        smartPager.RowCount = TotalRow;
        smartPager.UrlFormatString = string.Format("RoomTypeList.aspx?RoomTypeId={0}&IsVisible={1}&IsHome={2}&Expression={3}&Collate={4}", RoomTypeId, CommonHelper.BoolToIntString(IsVisible)
            , CommonHelper.BoolToIntString(IsHome), Expression, Collate) + "&PageIndex={0}";
        smartPager.Visible = TotalRow > PageSize;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //GetPropertyFromForm();
        //string Url = string.Format("RoomList.aspx?RoomTypeId={0}&IsVisible={1}&Expression={2}&Collate={3}", RoomTypeId, CommonHelper.BoolToIntString(IsVisible)
        //    , CommonHelper.BoolToIntString(IsHome), Expression, Collate);
        //Response.Redirect(Url);
    }
    protected void grv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        var hdfID = (HiddenField)grv.Rows[e.RowIndex].FindControl("hdfID");
        var hdfTourTypeId = (HiddenField)grv.Rows[e.RowIndex].FindControl("hdfRoomTypeId");
        try
        {
            UpdateOrderOffAndDelete(hdfID.Value, hdfTourTypeId.Value, 10000);
            TblRoomManager.CreateInstant().Delete(hdfID.Value.ToInt());
            Response.Redirect(Request.RawUrl);
            Trace.Warn("Delete Success" + hdfID.Value);
        }
        catch (Exception ex)
        {
            Trace.Warn("Delete Error" + hdfID.Value + ex.Message);
        }
        //GetPropertyFromForm();
        LoadData();
        //Response.Redirect(Request.RawUrl);
    }
    protected void btnUpdateOrder_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < grv.Rows.Count; i++)
        {
            var txtOrder = (TextBox)grv.Rows[i].FindControl("txtOrder");
            var hdfOrder = (HiddenField)grv.Rows[i].FindControl("hdfOrder");
            var hdfTourTypeId = (HiddenField)grv.Rows[i].FindControl("hdfRoomTypeId");
            var hdfID = (HiddenField)grv.Rows[i].FindControl("hdfID");
            int OldIndex = int.Parse(hdfOrder.Value);
            int NewIndex = int.Parse(txtOrder.Text);
            if (OldIndex != NewIndex)
                UpdateOrderOffAndDelete(hdfID.Value, hdfTourTypeId.Value, NewIndex);
        }
        Response.Redirect(Request.RawUrl);
    }
    private void UpdateOrderOffAndDelete(string strId, string strParentId, int NewIndex)
    {
        string Whereclause = " And RoomTypeId IS NULL ";
        if (!strParentId.Equals(""))
        {
            Whereclause = String.Format(" And RoomTypeId={0}", strParentId);
        }
        string Whereclause1 = String.Format(" Id={0}", strId);
        string Whereclause2 = String.Format(" And Id<>{0}", strId);
        Tools.SqlHelper.UpdateOrders("TblRoom", Whereclause, Whereclause1, Whereclause2, NewIndex);
    }
    protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {//
        //GetPropertyFromForm();
        grv.PageIndex = e.NewPageIndex;
        LoadData();
    }
}